package com.subframe7536.sa.controller;

import com.subframe7536.sa.Aspect.BusinessType;
import com.subframe7536.sa.Aspect.Log;
import org.example.service.base.ResponseResult;
import org.example.service.VO.PhoneLoginVO;
import org.example.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author subframe7536
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class LoginController {

    @Resource
    public LoginService loginService;

//    @Resource
//    public WeixinConfig config;

    @Log(title = "login", businessType = BusinessType.OTHER)
    @PostMapping("/login")
    public ResponseResult<String> login(@RequestBody PhoneLoginVO one) {
        return loginService.login(one);
    }

    @Log(title = "logout", businessType = BusinessType.OTHER)
    @PostMapping("/logout")
    public ResponseResult<String> logout(HttpServletRequest request) {
        return loginService.logout(request.getHeader("token"));
    }

//    @PostMapping("/wxlogin")
//    public void wxLogin(HttpServletResponse response) throws IOException {
//        // TODO: 2022/3/11 微信开放平台需公众号
//        //这个url的域名必须在公众号中进行注册验证，这个地址是成功后的回调地址
//        String backUrl = "http://" +
//            config.getDomain() + "/wxLoginCallback";
//        // 第一步：用户同意授权，获取code
//        //请求地址  snsapi_base   snsapi_userinfo
//        String url="https://open.weixin.qq.com/connect/qrconnect?" +
//            "appid=" +config.getAppid() +
//            "&redirect_uri=" +URLEncoder.encode(backUrl, "utf-8") +
//            "&response_type=code" +
//            "&scope=snsapi_login" +
//            "&state=STATE" +
//            "#wechat_redirect";
////        String url = "https://open.weixin.qq.com/connect/oauth2/authorize" +
////            "?appid=" +
////            "&redirect_uri=" +
////            "&response_type=code" +
////            "&scope=snsapi_userinfo" +
////            "&state=STATE#wechat_redirect";
//        //必须重定向，否则不能成功
//        response.sendRedirect(url);
//    }
//
//    @RequestMapping("/wxLoginCallback")
//    public LoginEntity callback(HttpServletRequest req) {
//        LoginEntity userLoginRes = new LoginEntity();
//        try {
//            /*start 获取微信用户基本信息*/
//            String code = req.getParameter("code");
//            //第二步：通过code换取网页授权access_token
//            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
//                + "appid=" + config.getAppid()
//                + "&secret=" + config.getAppSecret()
//                + "&code=" + code
//                + "&grant_type=authorization_code";
//            System.out.println(url);
//            String result = WeiXinHttpClientUtil.doGet(url);
//            Map<String, Object> jsonResponse = JsonUtil.jsonToMap(result);
//
//            /*
//         {   "access_token":"ACCESS_TOKEN",
//            "expires_in":7200,
//            "refresh_token":"REFRESH_TOKEN",
//            "openid":"OPENID",
//            "scope":"SCOPE"
//           }
//         */
//            String openId = (String) jsonResponse.get("openid");
//            String accessToken = (String) jsonResponse.get("access_token");
//            //第三步验证access_token是否失效；
//            String checkUrl = "https://api.weixin.qq.com/sns/auth?access_token="
//                + accessToken + "&openid=" + openId;
//            result = WeiXinHttpClientUtil.doGet(checkUrl);
//            jsonResponse = JsonUtil.jsonToMap(result);
//            System.out.println(jsonResponse.toString());
//            if (!"0".equals(jsonResponse.get("errcode"))) {
//                result = WeiXinHttpClientUtil.doGet(checkUrl);
//                jsonResponse = JsonUtil.jsonToMap(result);
//             /*
//              { "access_token":"ACCESS_TOKEN",
//                "expires_in":7200,
//                "refresh_token":"REFRESH_TOKEN",
//                "openid":"OPENID",
//                "scope":"SCOPE" }
//             */
//                accessToken = (String) jsonResponse.get("access_token");
//            }
//            // 第四步：拉取用户信息
//            String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken
//                + "&openid=" + openId
//                + "&lang=zh_CN";
//            jsonResponse = JsonUtil.jsonToMap(WeiXinHttpClientUtil.doGet(infoUrl));
//            /*
//         {  "openid":" OPENID",
//            "nickname": NICKNAME,
//            "sex":"1",
//            "province":"PROVINCE"
//            "city":"CITY",
//            "country":"COUNTRY",
//            "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
//            "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
//            "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
//           }
//         */
//            System.out.println(jsonResponse);
//            userLoginRes.setOpenId((String) jsonResponse.get("openid"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return userLoginRes;
//    }

}
